package com.server.db.bean;

import com.sandking.tools.SK_Map;
import java.util.ArrayList;
import com.sandking.config.SK_BasePojo;
import com.sandking.config.SK_Config;
import com.alibaba.fastjson.JSON;
import java.util.Map;
import com.sandking.io.SK_ByteBuff;
import com.sandking.tools.SK_Plus;
import java.util.List;
import com.server.db.imp.YxImp;
import com.server.db.imp.WjImp;
import java.util.HashMap;

/**
 * 玩家
 */
public class Wj extends SK_BasePojo {

	public static final String TABLE_NAME = "玩家";
	public static final String CLASS_NAME = "Wj"; 
	
	/** id */
	private String id;
	
	/** 帐号 */
	private String zh;
	
	/** 密码 */
	private String mm;
	
	/** 性别 */
	private int xb;
	
	/** 类型 */
	private int lx;
	
	/** 服务器id */
	private int fwqid;
	
	/** 创建时间 */
	private java.util.Date cjsj;
	
	/** 最后登录时间 */
	private java.util.Date zhdlsj;
	
	
	public Wj() {
		super();
		set_status(SK_Config.SOURCE_OBJECT);
	}
	
	public Wj(String id, String zh, String mm, int xb, int lx, int fwqid, java.util.Date cjsj, java.util.Date zhdlsj) {
		super();
		this.id = id;
		this.zh = zh;
		this.mm = mm;
		this.xb = xb;
		this.lx = lx;
		this.fwqid = fwqid;
		this.cjsj = cjsj;
		this.zhdlsj = zhdlsj;
		set_status(SK_Config.SOURCE_OBJECT);
	}
	
	public String getId() {
		return id;
	}
	
	public void setId(String id) {
		changeSetId(id);
		if(get_status() != SK_Config.SOURCE_OBJECT){
			getByteBuff().writeInt(1);
			getByteBuff().writeString(id);
		}
	}
	
	private void changeSetId(String id) {
		this.id = id;
		if(get_status() != SK_Config.SOURCE_OBJECT){
			addUpdateColumn("id",id);
		}
	}
	
	public String getZh() {
		return zh;
	}
	
	public void setZh(String zh) {
		changeSetZh(zh);
		if(get_status() != SK_Config.SOURCE_OBJECT){
			getByteBuff().writeInt(2);
			getByteBuff().writeString(zh);
		}
	}
	
	private void changeSetZh(String zh) {
		this.zh = zh;
		if(get_status() != SK_Config.SOURCE_OBJECT){
			addUpdateColumn("帐号",zh);
		}
	}
	
	public String getMm() {
		return mm;
	}
	
	public void setMm(String mm) {
		changeSetMm(mm);
		if(get_status() != SK_Config.SOURCE_OBJECT){
			getByteBuff().writeInt(3);
			getByteBuff().writeString(mm);
		}
	}
	
	private void changeSetMm(String mm) {
		this.mm = mm;
		if(get_status() != SK_Config.SOURCE_OBJECT){
			addUpdateColumn("密码",mm);
		}
	}
	
	public int getXb() {
		return xb;
	}
	
	public void setXb(int xb) {
		changeSetXb(xb);
		if(get_status() != SK_Config.SOURCE_OBJECT){
			getByteBuff().writeInt(4);
			getByteBuff().writeInt(xb);
		}
	}
	
	private void changeSetXb(int xb) {
		this.xb = xb;
		if(get_status() != SK_Config.SOURCE_OBJECT){
			addUpdateColumn("性别",xb);
		}
	}
	
	public void changeXbWith(int xb){
		this.xb += xb;
		changeSetXb(this.xb);
		//不是源对象才记录变更操作
		if(get_status() != SK_Config.SOURCE_OBJECT){
			getByteBuff().writeInt(5);
			getByteBuff().writeInt(xb);
		}
	}
	public void changeXbWithMin(int xb,int min){
		this.xb += xb;
		this.xb = this.xb < min ? min : this.xb;
		changeSetXb(this.xb);
		//不是源对象才记录变更操作
		if(get_status() != SK_Config.SOURCE_OBJECT){
			getByteBuff().writeInt(6);
			getByteBuff().writeInt(xb);
			getByteBuff().writeInt(min);
		}	
	}
	public void changeXbWithMax(int xb,int max){
		this.xb += xb;
		this.xb = this.xb > max ? max : this.xb;
		changeSetXb(this.xb);
		//不是源对象才记录变更操作
		if(get_status() != SK_Config.SOURCE_OBJECT){
			getByteBuff().writeInt(7);
			getByteBuff().writeInt(xb);
			getByteBuff().writeInt(max);
		}
	}
	public void changeXbWithMaxMin(int xb,int max,int min){
		this.xb += xb;
		this.xb = this.xb < min ? min : this.xb;
		this.xb = this.xb > max ? max : this.xb;
		changeSetXb(this.xb);
		//不是源对象才记录变更操作
		if(get_status() != SK_Config.SOURCE_OBJECT){
			getByteBuff().writeInt(8);
			getByteBuff().writeInt(xb);
			getByteBuff().writeInt(max);
			getByteBuff().writeInt(min);
		}	
	}	
	public int getLx() {
		return lx;
	}
	
	public void setLx(int lx) {
		changeSetLx(lx);
		if(get_status() != SK_Config.SOURCE_OBJECT){
			getByteBuff().writeInt(9);
			getByteBuff().writeInt(lx);
		}
	}
	
	private void changeSetLx(int lx) {
		this.lx = lx;
		if(get_status() != SK_Config.SOURCE_OBJECT){
			addUpdateColumn("类型",lx);
		}
	}
	
	public void changeLxWith(int lx){
		this.lx += lx;
		changeSetLx(this.lx);
		//不是源对象才记录变更操作
		if(get_status() != SK_Config.SOURCE_OBJECT){
			getByteBuff().writeInt(10);
			getByteBuff().writeInt(lx);
		}
	}
	public void changeLxWithMin(int lx,int min){
		this.lx += lx;
		this.lx = this.lx < min ? min : this.lx;
		changeSetLx(this.lx);
		//不是源对象才记录变更操作
		if(get_status() != SK_Config.SOURCE_OBJECT){
			getByteBuff().writeInt(11);
			getByteBuff().writeInt(lx);
			getByteBuff().writeInt(min);
		}	
	}
	public void changeLxWithMax(int lx,int max){
		this.lx += lx;
		this.lx = this.lx > max ? max : this.lx;
		changeSetLx(this.lx);
		//不是源对象才记录变更操作
		if(get_status() != SK_Config.SOURCE_OBJECT){
			getByteBuff().writeInt(12);
			getByteBuff().writeInt(lx);
			getByteBuff().writeInt(max);
		}
	}
	public void changeLxWithMaxMin(int lx,int max,int min){
		this.lx += lx;
		this.lx = this.lx < min ? min : this.lx;
		this.lx = this.lx > max ? max : this.lx;
		changeSetLx(this.lx);
		//不是源对象才记录变更操作
		if(get_status() != SK_Config.SOURCE_OBJECT){
			getByteBuff().writeInt(13);
			getByteBuff().writeInt(lx);
			getByteBuff().writeInt(max);
			getByteBuff().writeInt(min);
		}	
	}	
	public int getFwqid() {
		return fwqid;
	}
	
	public void setFwqid(int fwqid) {
		changeSetFwqid(fwqid);
		if(get_status() != SK_Config.SOURCE_OBJECT){
			getByteBuff().writeInt(14);
			getByteBuff().writeInt(fwqid);
		}
	}
	
	private void changeSetFwqid(int fwqid) {
		this.fwqid = fwqid;
		if(get_status() != SK_Config.SOURCE_OBJECT){
			addUpdateColumn("服务器id",fwqid);
		}
	}
	
	public void changeFwqidWith(int fwqid){
		this.fwqid += fwqid;
		changeSetFwqid(this.fwqid);
		//不是源对象才记录变更操作
		if(get_status() != SK_Config.SOURCE_OBJECT){
			getByteBuff().writeInt(15);
			getByteBuff().writeInt(fwqid);
		}
	}
	public void changeFwqidWithMin(int fwqid,int min){
		this.fwqid += fwqid;
		this.fwqid = this.fwqid < min ? min : this.fwqid;
		changeSetFwqid(this.fwqid);
		//不是源对象才记录变更操作
		if(get_status() != SK_Config.SOURCE_OBJECT){
			getByteBuff().writeInt(16);
			getByteBuff().writeInt(fwqid);
			getByteBuff().writeInt(min);
		}	
	}
	public void changeFwqidWithMax(int fwqid,int max){
		this.fwqid += fwqid;
		this.fwqid = this.fwqid > max ? max : this.fwqid;
		changeSetFwqid(this.fwqid);
		//不是源对象才记录变更操作
		if(get_status() != SK_Config.SOURCE_OBJECT){
			getByteBuff().writeInt(17);
			getByteBuff().writeInt(fwqid);
			getByteBuff().writeInt(max);
		}
	}
	public void changeFwqidWithMaxMin(int fwqid,int max,int min){
		this.fwqid += fwqid;
		this.fwqid = this.fwqid < min ? min : this.fwqid;
		this.fwqid = this.fwqid > max ? max : this.fwqid;
		changeSetFwqid(this.fwqid);
		//不是源对象才记录变更操作
		if(get_status() != SK_Config.SOURCE_OBJECT){
			getByteBuff().writeInt(18);
			getByteBuff().writeInt(fwqid);
			getByteBuff().writeInt(max);
			getByteBuff().writeInt(min);
		}	
	}	
	public java.util.Date getCjsj() {
		return cjsj;
	}
	
	public void setCjsj(java.util.Date cjsj) {
		changeSetCjsj(cjsj);
		if(get_status() != SK_Config.SOURCE_OBJECT){
			getByteBuff().writeInt(19);
			getByteBuff().writeDate(cjsj);
		}
	}
	
	private void changeSetCjsj(java.util.Date cjsj) {
		this.cjsj = cjsj;
		if(get_status() != SK_Config.SOURCE_OBJECT){
			addUpdateColumn("创建时间",cjsj);
		}
	}
	
	public java.util.Date getZhdlsj() {
		return zhdlsj;
	}
	
	public void setZhdlsj(java.util.Date zhdlsj) {
		changeSetZhdlsj(zhdlsj);
		if(get_status() != SK_Config.SOURCE_OBJECT){
			getByteBuff().writeInt(20);
			getByteBuff().writeDate(zhdlsj);
		}
	}
	
	private void changeSetZhdlsj(java.util.Date zhdlsj) {
		this.zhdlsj = zhdlsj;
		if(get_status() != SK_Config.SOURCE_OBJECT){
			addUpdateColumn("最后登录时间",zhdlsj);
		}
	}
	
	
	//id
	public List<Yx> getYxsFkWjid(){
		return YxImp.getByWjid(id);
	}
	
	public void addUpdateColumn(String key, Object val) {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.put(key, val);
	}
	/**
	public void clearUpdateColumn() {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.clear();
	}
	*/
	public Wj clone(){
		Wj wj = new Wj();
		wj.setId(this.getId());
		wj.setZh(this.getZh());
		wj.setMm(this.getMm());
		wj.setXb(this.getXb());
		wj.setLx(this.getLx());
		wj.setFwqid(this.getFwqid());
		wj.setCjsj(this.getCjsj());
		wj.setZhdlsj(this.getZhdlsj());
		wj.set_v(get_v());
		wj.set_status(SK_Config.CLONE_OBJECT);
		return wj.updatePrimaryIndex(); 
	}
	
	public Map<String, Object> toMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("zh", this.zh);
        result.put("mm", this.mm);
        result.put("xb", this.xb);
        result.put("lx", this.lx);
        result.put("fwqid", this.fwqid);
        result.put("cjsj", this.cjsj);
        result.put("zhdlsj", this.zhdlsj);
        return result;
    }
    
    public String toString(){
        return toMap().toString();
    }
    
    public String toJson(){
    	return JSON.toJSONString(toMap());
    }
    
    /**
     * 数据库源字段Map
     */
    public Map<String, Object> toColumnNameMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("帐号", this.zh);
        result.put("密码", this.mm);
        result.put("性别", this.xb);
        result.put("类型", this.lx);
        result.put("服务器id", this.fwqid);
        result.put("创建时间", this.cjsj);
        result.put("最后登录时间", this.zhdlsj);
        return result;
    }
    
    public String toColumnNameString(){
        return toColumnNameMap().toString();
    }
    
    public byte[] toBytes() throws Exception {
    	SK_ByteBuff byteBuff = SK_ByteBuff.allocate();
	    byteBuff.writeString(this.id);
	    byteBuff.writeString(this.zh);
	    byteBuff.writeString(this.mm);
	    byteBuff.writeInt(this.xb);
	    byteBuff.writeInt(this.lx);
	    byteBuff.writeInt(this.fwqid);
	    byteBuff.writeDate(this.cjsj);
	    byteBuff.writeDate(this.zhdlsj);
	    return byteBuff.toByte();
    }
    
    public byte[] toSKBytes(){
    	SK_ByteBuff byteBuff = SK_ByteBuff.allocate();
    	byteBuff.writeMap(toMap());
	    return byteBuff.toByte();
    }
    
     public static Wj createForBytes(byte[] _byte) throws Exception {
     	SK_ByteBuff byteBuff = SK_ByteBuff.wrap(_byte);
     	Wj wj = new Wj();
	    wj.id = byteBuff.readString(null);
	    wj.zh = byteBuff.readString(null);
	    wj.mm = byteBuff.readString(null);
	    wj.xb = byteBuff.readInt(null);
	    wj.lx = byteBuff.readInt(null);
	    wj.fwqid = byteBuff.readInt(null);
	    wj.cjsj = byteBuff.readDate(null);
	    wj.zhdlsj = byteBuff.readDate(null);
	    return wj.updatePrimaryIndex();
     }
     
     public static Wj createForSKBytes(byte[] _byte){
     		SK_ByteBuff byteBuff = SK_ByteBuff.wrap(_byte);
     		@SuppressWarnings("unchecked")
     		Map<String,Object> map = byteBuff.readMap(null);
	     	Wj wj = Wj.createForMap(map);
		    return wj.updatePrimaryIndex();
     }
    
    /**
     * 根据list创建对象
     */
    public static List<Wj> createForColumnNameList(List<Map<String, Object>> list){
    	List<Wj> wjs = new ArrayList<Wj>();
		for (Map<String, Object> map : list) {
			wjs.add(createForColumnNameMap(map));
		}
		return wjs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Wj createForColumnNameMap(Map<String, Object> map){
    	Wj obj = new Wj();
	    obj.id = SK_Map.getString("id", map);
	    obj.zh = SK_Map.getString("帐号", map);
	    obj.mm = SK_Map.getString("密码", map);
	    obj.xb = SK_Map.getInt("性别", map);
	    obj.lx = SK_Map.getInt("类型", map);
	    obj.fwqid = SK_Map.getInt("服务器id", map);
	    obj.cjsj = SK_Map.getDate("创建时间", map);
	    obj.zhdlsj = SK_Map.getDate("最后登录时间", map);
        return obj.updatePrimaryIndex();
    }
    
    /**
     * 根据list创建对象
     */
    public static List<Wj> createForList(List<Map<String, Object>> list){
    	List<Wj> wjs = new ArrayList<Wj>();
		for (Map<String, Object> map : list) {
			wjs.add(createForColumnNameMap(map));
		}
		return wjs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Wj createForMap(Map<String, Object> map){
    	Wj obj = new Wj();
	    obj.id = SK_Map.getString("id", map);
	    obj.zh = SK_Map.getString("zh", map);
	    obj.mm = SK_Map.getString("mm", map);
	    obj.xb = SK_Map.getInt("xb", map);
	    obj.lx = SK_Map.getInt("lx", map);
	    obj.fwqid = SK_Map.getInt("fwqid", map);
	    obj.cjsj = SK_Map.getDate("cjsj", map);
	    obj.zhdlsj = SK_Map.getDate("zhdlsj", map);
        return obj.updatePrimaryIndex();
    }
    
    public static Wj createForJson(String json){
    	Map<String, Object> map = JSON.parseObject(json);
    	return createForMap(map);
    }
    
    public static List<Wj> createForJson(List<String> jsons){
    	List<Wj> wjs = new ArrayList<Wj>();
    	for(String json : jsons){
    		wjs.add(createForJson(json));
    	}
    	return wjs;
    }
    
    /** 级联删除(延迟入库) */
    public boolean deleteAndSon(){
    	return false;
    }
    
    /** 级联删除(及时入库) */
    public boolean deleteAndSonAndFlush(){
    	return false;
    }
    
    /** 延迟插入数据库 */
    public Wj insert(){
    	return WjImp.insert(this);
    }
    /** 延迟更新数据库 */
    public Wj update(){
    	return WjImp.update(this);
    }
    /** 延迟删除数据库 */
    public boolean delete(){
    	return WjImp.delete(this);
    }
    /** 即时插入数据库 */
    public Wj insertAndFlush(){
    	return WjImp.insertAndFlush(this);
    }
    /** 即时更新数据库 */
    public Wj updateAndFlush(){
    	return WjImp.updateAndFlush(this);
    }
    /** 即时删除数据库 */
    public boolean deleteAndFlush(){
    	return WjImp.deleteAndFlush(this);
    }
    
    
    
    //--------------------------------Cache 中的索引 GET & SET
	public String getId_Unique(){
		return id_Unique;
	}
	
	public void setId_Unique(String id_Unique){
		this.id_Unique = id_Unique;
	}
	
	public String getXb_Index(){
		return xb_Index;
	}
	
	public void setXb_Index(String xb_Index){
		this.xb_Index = xb_Index;
	}
	
	public String getFwqidLx_Index(){
		return fwqidlx_Index;
	}
	
	public void setFwqidLx_Index(String fwqidlx_Index){
		this.fwqidlx_Index = fwqidlx_Index;
	}
	
	public String getLx_Index(){
		return lx_Index;
	}
	
	public void setLx_Index(String lx_Index){
		this.lx_Index = lx_Index;
	}
	
	public String getZh_Unique(){
		return zh_Unique;
	}
	
	public void setZh_Unique(String zh_Unique){
		this.zh_Unique = zh_Unique;
	}
	
	public String getZhMm_Unique(){
		return zhmm_Unique;
	}
	
	public void setZhMm_Unique(String zhmm_Unique){
		this.zhmm_Unique = zhmm_Unique;
	}
	
	
	
	/** 取得改变的索引 */
	public Map<String,String> getUpdateIndexs(){
		Map<String,String> indexNames = new HashMap<String,String>();
		if(!this.getId_Unique().equals(SK_Plus.b(id).e())){
			indexNames.put(Wj.Id_IndexName,SK_Plus.b(id).e());
			indexNames.put("isUpdtaeUnique","true");
		}
			
		if(!this.getXb_Index().equals(SK_Plus.b(xb).e()))
			indexNames.put(Wj.Xb_IndexName,SK_Plus.b(xb).e());
			
		if(!this.getFwqidLx_Index().equals(SK_Plus.b(fwqid,"_",lx).e()))
			indexNames.put(Wj.FwqidLx_IndexName,SK_Plus.b(fwqid,"_",lx).e());
			
		if(!this.getLx_Index().equals(SK_Plus.b(lx).e()))
			indexNames.put(Wj.Lx_IndexName,SK_Plus.b(lx).e());
			
		if(!this.getZh_Unique().equals(SK_Plus.b(zh).e())){
			indexNames.put(Wj.Zh_IndexName,SK_Plus.b(zh).e());
			indexNames.put("isUpdtaeUnique","true");
		}
			
		if(!this.getZhMm_Unique().equals(SK_Plus.b(zh,"_",mm).e())){
			indexNames.put(Wj.ZhMm_IndexName,SK_Plus.b(zh,"_",mm).e());
			indexNames.put("isUpdtaeUnique","true");
		}
			
		return indexNames;
	}
	
	//索引名称
	public static final String  Id_IndexName = "id";
	public static final String  Xb_IndexName = "xb";
	public static final String  FwqidLx_IndexName = "fwqidlx";
	public static final String  Lx_IndexName = "lx";
	public static final String  Zh_IndexName = "zh";
	public static final String  ZhMm_IndexName = "zhmm";
	
	
	//Cache 中的索引
	private String  id_Unique = null;
	private String  xb_Index = null;
	private String  fwqidlx_Index = null;
	private String  lx_Index = null;
	private String  zh_Unique = null;
	private String  zhmm_Unique = null;
	public Wj updatePrimaryIndex(){
		return updatePrimaryIndex(this);
	}
	
	public static List<Wj> updatePrimaryIndex(List<Wj> wjs){
		for(Wj wj : wjs){
			updatePrimaryIndex(wj);	
		}
		return wjs;
	}
	
	public static Wj updatePrimaryIndex(Wj wj){
		String field = SK_Plus.b(wj.getId()).e();
		wj.setId_Unique(field);
		
		field = SK_Plus.b(wj.getXb()).e();
		wj.setXb_Index(field);
		
		field = SK_Plus.b(wj.getFwqid(),"_",wj.getLx()).e();
		wj.setFwqidLx_Index(field);
		
		field = SK_Plus.b(wj.getLx()).e();
		wj.setLx_Index(field);
		
		field = SK_Plus.b(wj.getZh()).e();
		wj.setZh_Unique(field);
		
		field = SK_Plus.b(wj.getZh(),"_",wj.getMm()).e();
		wj.setZhMm_Unique(field);
		
		return wj;
	}
}